Using Regular Expressions in Macromedia Central
Table of Contents
Implementing Regular Expressions in Central
The implementation of regular expressions in Macromedia Central follows the ECMA-262 Standard of the ECMAScript Language Specification. The two ways in which you use regular expressions is through the new RegExp class or through an added method to the String class. In the following sections I walk you through what you need to know about both.
RegExp Object
You may have noticed the RegExp constructor used in the previous code samples. It creates a RegExp object that accepts a string, which you use to define the pattern of the expression, and a second optional parameter in which you can easily set properties for the resulting RegExp object. Here are some examples:
re1 = new RegExp("(\\w)\\1"); // regular expression that matches any two consecutive "word" characters
re2 = new RegExp("hello world", "i"); // matches the string "hello world" and is case-insensitive due to the 'i' flag (explained below)
RegExp Properties
Among the handful of properties of the RegExp object are several Boolean properties that allow you to alter the default behavior. The default value for all flags is false (see Table 4).
| Flag | Name |
|---|---|
i |
ignoreCase |
s |
dotall |
m |
multiline |
x |
extended |
g |
global |
Note: All the Boolean properties in Table 4 are read-only and default to false. They can only be set to true by passing the corresponding flag into the second parameter of the constructor.
The ignoreCase property is perhaps the easiest to understand. By adding an "i" to the second parameter when you construct a RegExp object, the ignoreCase property is set to true. With this property set, a character literal now matches either upper- or lowercase words. Here is an example of its usage:
re = new RegExp("hello world", "i");
trace(re.ignoreCase); // outputs true
re.test("Hello World"); // returns true
re.test("hello world"); // returns true
re.test("HELLO WORLD"); // returns true
The dotall property is also relatively easy to understand. The default behavior of the RegExp object is for the dot (.) metacharacter to not match a newline character (\n). When the dotall property is set to true, using the "s" flag, the dot matches the newline character:
var testString = "hello blah\nblah world";
var re = new RegExp("hello.*world");
trace(re.dotall); // outputs false
re.test(testString); // returns false, the . does not match the \n
var re2 = new RegExp("hello.*world ", "s");
trace(re2.dotall); // outputs true
re2.test(testString); // returns true
If you want to set two Boolean properties to true, pass both flags into the second parameter of the RegExp constructor:
var re = new RegExp("hello", "si"); // order of flags does not matter
trace(re.ignoreCase); // outputs true
trace(re.dotall); // outputs true
The multiline property is interesting in that it changes the meaning
of the ^ and $ anchors. When multiline=false, the ^ matches
the beginning of the entire string and the $ matches the end of the
entire string. However, when multiline=true, each line
within the string has its own line-end anchors. When a RegExp object
has this setting, ^ matches the beginning of the entire string or the
beginning of a new line (after a \n character). Similarly, $ matches
the end of the entire string or the end of a line (before
a \n character). For example:
Var testString = "hello\nworld";
Var re1 = new RegExp("^hello$");
Var re2 = new RegExp("^hello$", "m");
Re1.test(testString); // returns false; the $ is not matched because the end of the string is not reached
Re2.test(testString); // returns true; the $ is matched because the end of a line is reached
The extended property simply ignores any whitespace within the first parameter to the RegExp constructor. To include whitespace in the regular expression when using the extended property, use a whitespace metacharacter such as \\s:
var re1 = new RegExp("hello world", "x");
var re2 = new RegExp("hello\\sworld", "x");
re1.test("hello world"); // returns false because the space in the constructor string was ignored
re1.test("helloworld"); // returns true
re2.test("hello world"); // return true; used the whitespace metacharacter \\s for the space
The related global and lastIndex properties are probably the most difficult to grasp. When the global property is set to true, method calls on a RegExp object pick up where the prior call left off. This is done by starting the search at one plus the index that was found in the previous search. To do this, the lastIndex property is used. Whenever a RegExp method is called, the index of the last character of the string that is matched plus one is stored in the lastIndex property. If no match is found, or if the global property is false, then lastIndex is set to zero.
Here are some examples to illustrate these properties:
var testString = "hello hello";
re = new RegExp("hello", "g");
trace(re.global); // outputs true
trace(re.lastIndex); // outputs 0
re.test(testString); // returns true; starting at 0 this method finds a match on the characters between indexes 0 and 4
trace(re.lastIndex); // outputs 5; the last index matched was 4 (remember we count from 0) and then we add one
// since global was set to true the next time we call a method with this RegExp it starts from the index stored in the lastIndex property
re.test(testString); // returns true; starts looking at 5 and finds a match between index 6 and 10
trace(re.lastIndex); // outputs 11
re.test(testString); // returns false; there are no more matches in the string after the 11th character
trace(re.lastIndex); // outputs 0; because no match was found, the lastIndex property is reset to 0
In addition to lastIndex, there is another non-Boolean property of a RegExp object, called source. This property holds the value of the first parameter used when constructing the RegExp. A possible use for this property would be to compare RegExp objects for equality:
var re = new RegExp("hi");
trace(re.source); // outputs "hi"
RegExp Methods
The most basic method of a RegExp object is match(), which accepts a string as input and returns the portion of the string that was matched by the regular expression if a match exists and returns null if none exists. For example:
var re = new RegExp("\\bh\\w+\\b");
re.match("hello world"); // returns "hello"
re.match("my name is haldir"); // returns "haldir"
re.match("this won't match?"); // returns null
I have used the test() command throughout this article. It is simply a wrapper around the match() command: If match() returns null, then test() returns false; otherwise test() returns true. Using the examples above, we now have the following expressions:
var re = new RegExp("\\bh\\w+\\b");
re.test("hello world"); // returns true
re.test("my name is haldir"); // returns true
re.test("this won't match?"); // returns false
I previously mentioned the memory syntax using the "( )" metacharacters. These now play a new role with the match() command. The match() command actually returns an array of values but this array generally only has one element that is treated as a string. However, if you use the memory metacharacters, you can pass additional values out of the match() command. Let's say you want to match the pattern "My name is {someName}" but all you really care about is the name. In this case you can use the memory to return the name in a separate array element. For example:
var re = new RegExp("My name is \\b(\\w+)\\b");
re.match("His name is Tom and My name is Byron"); // returns "My name is Byron", "Byron"
The first element in the returned array is the whole string that is matched; the subsequent elements are the ordered memory groups, starting with their appearance from left to right. Another useful method for using regular expressions is to find some portion of text that matches a pattern and then replace it with another string. The RegExp.prototype.replace() method allows you to do this. It accepts two parameters: the string to search through and the replacement string. A new string with the replacements made is then returned:
// curse word filter
var text = "hello there, I hope that you all are doing well";
var re = new RegExp("\\b\\w{4}\\b");
re.replace(text, "****"); // outputs "hello there, I **** **** you all are doing ****"
String Method
A new method has been added to the String object to make it more convenient to implement regular expressions. The String object now features the replace() command, which is a wrapper around the replace() method. The only difference is that the first parameter for this replace() method is a source string to define a regular expression, the same as you would use in the RegExp constructor:
// curse word filter using String.replace()
var text = "hello there, I hope that you all are doing well";
text.replace("\\b\\w{4}\\b", "****"); // replace all four-letter words with ****